 
import axios from 'axios'
import { Toast } from 'vant'
import router from '../router'
// axios.defaults.baseURL = process.env.NODE_ENV == 'development' ? '//backend-api-01.newbee.ltd/api/v1' : '//backend-api-01.newbee.ltd/api/v1'
axios.defaults.baseURL =  "http://47.97.99.8:8080";
// axios.defaults.withCredentials = true
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers['userToken'] = localStorage.getItem('token') || ''
let info=JSON.parse(localStorage.getItem('userinfo'));
if(info)axios.defaults.headers['userId'] =  info.id || ''
axios.defaults.headers.post['Content-Type'] = 'application/json' 
axios.interceptors.response.use(res => { 
  if (typeof res.data !== 'object') {
    Toast.fail('服务端异常！')
    return Promise.reject(res)
  }
  if(res.data.status== 502||res.data.status== 501)
  {
    Toast.fail('会话失效，请重新登录！')
    router.push({name:"login"})
    return Promise.reject(res)
  }
  if (res.data.status!= 200) {
    if (res.data.msg) Toast.fail(res.data.msg) 
    return Promise.reject(res.data)
  } 
  
  return res.data
}) 
export default axios
